package com.shj.controller;

import com.alibaba.fastjson.JSONObject;
import com.shj.entity.SysPart;
import com.shj.entity.SysPartRecord;
import com.shj.entity.SysPerson;
import com.shj.service.SysPartRecordService;
import com.shj.service.SysPartService;
import com.shj.service.SysPersonService;
import com.shj.util.MyIdUtil;
import com.shj.util.MyTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.ejb.Local;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @author songhuajian
 * @Description: TODO
 * @date 2020/6/16 15:25
 **/
@Controller
@RequestMapping("/biz/record")
public class SysPartRecordController {
    @Autowired
    private SysPartRecordService sysPartRecordService;
    @Autowired
    private SysPersonService sysPersonService;
    @Autowired
    private SysPartService sysPartService;

    @RequestMapping("/list")
    public String findAll(Model model) {
        List<SysPartRecord> list = sysPartRecordService.findAll();
        model.addAttribute("list", list);
        return "record/record_list";
    }

    @RequestMapping("/openPersonList")
    public String openPersonList(Model model) {
        // 判断是否设置参合缴费标准
        LocalDateTime now = MyTimeUtil.getNow();
        SysPart sysPart = sysPartService.findByYear(String.valueOf(now.getYear()));
        if (sysPart == null) {
            model.addAttribute("msg", "未设置参合缴费标准");
            return findAll(model);
        } else if (!MyTimeUtil.isCalendar(now, sysPart.getPartStartTime(), sysPart.getPartEndTime())) {
            model.addAttribute("msg", "不在参合缴费时间内");
            return findAll(model);
        }
        return "record/record_person_list";
    }

    @RequestMapping("/findPerson")
    public String findPerson(Model model, String personName) {
        List<SysPerson> list = sysPersonService.findHolder(personName);
        model.addAttribute("list", list);
        return "record/record_person_list";
    }

    @RequestMapping("/openPart")
    public String openPart(Model model, String famiCode) {
        List<SysPerson> list = sysPersonService.findNotPart(famiCode);
        LocalDateTime now = MyTimeUtil.getNow();
        SysPart sysPart = sysPartService.findByYear(String.valueOf(now.getYear()));
        model.addAttribute("part", sysPart);
        model.addAttribute("list", list);
        model.addAttribute("famiCode", list.get(0).getFamiCode());
        return "record/part_add_list";
    }

    @RequestMapping("/addPart")
    public String addPart(Model model, SysPartRecord sysPartRecord, String[] ids) {
        String recordId = MyIdUtil.getTimeId();
        sysPartRecord.setRecordId(recordId);
        String time = MyTimeUtil.getNowTime();
        sysPartRecord.setPartTime(time);
        sysPartRecordService.add(sysPartRecord);
        // 修改农民是否参合
        sysPersonService.addPart(ids);
        return findAll(model);
    }

}
